import requests
import json
import re
import os
if __name__=='__main__':
    header={'user-agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36'}
    if not os.path.exists('糗图图片库'):
        os.mkdir('糗图图片库')
    for i in range(1,7):
        url='https://www.qiushibaike.com/imgrank/page/{}/'.format(i)
        print(url)
        page_text=requests.get(url=url,headers=header).text
        ex='<div class="thumb">.*?<img src="(.*?)" alt='
        img_urls=re.findall(ex,page_text,re.S)

        for img_url in img_urls:
            img_url='https:'+img_url
            img_data=requests.get(url=img_url,headers=header).content
            img_name=img_url.split('/')[-1]
            with open('./糗图图片库/'+img_name,'wb') as fp:
                fp.write(img_data)





